<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class CurrencyController extends CommonController
{
    public function index()
    {
        $game_id = M('game')->select();
        $this->assign("game_id",$game_id);
        $this->display();
    }

    /**
     * 货币列表
     */
    public function currencyList(){
        $where = array();
        $sort = I('request.sort', 'orderid');
        $order = I('request.order', 'asc');
        $currency = I('currency');
        if(!empty($currency)) $where['g.currency'] = array('like','%'.$currency.'%');

        $result = M('currency as c')
                ->join('LEFT JOIN '.C('DB_PREFIX').'game as g ON c.gameid = g.gameid')
                ->field('c.*,g.game')
                ->where($where)
                ->where('c.is_delete=1')
                ->order($sort." ".$order)
                ->select();       
        foreach ($result as $k => &$v) {
            $v['gameid']  = $v['gameid'] ?  $v['gameid'] : '';
            $v['createtime']=date("Y-m-d H:i:s",$v['createtime']);
            $v['updatetime']=date("Y-m-d H:i:s",$v['updatetime']);
            $v['orderid']=(int)$v['orderid'];
        }
        $this->ajaxReturn($result);
    }

    /**
    * 修改增加货币
    * @author wang
    * @param 
    */
    public function msgOpt(){
        $curyid = I('curyid');

        $data = I();
        $data['admin'] = session('admin');
        $data['updatetime'] = time();

        if($curyid){
            if(M('currency')->where("curyid='$curyid'")->save($data)){
                $this->ajaxReturn(array('success' => 1, 'msg' => '用户修改成功','id'=>$curyid));
            }else{
                $this->ajaxReturn(array('success' => 0, 'msg' => '用户修改失败'));
            }
        }else{
            // $data['id'] = (new  \Org\Twitter\IdWork(31,31))->nextId();
            $data['createtime']  = time();
            if(M('currency')->add($data)){
                $this->ajaxReturn(array('success' => 1, 'msg' => '用户新增成功'));
            }else{
                $this->ajaxReturn(array('success' => 0, 'msg' => '用户增加失败'));
            }
        }
    }


    /**
     * 货币假删除
     */
    public function delCurrency(){
        $curyid = I('curyid');
        if(M('currency')->where("curyid='$curyid'")->save(['is_delete'=>0])){
            $result = array(
                'sta' => 1, 
                'msg' => '删除成功'
            );
        }else{
            $result = array(
                'sta' => 0, 
                'msg' => '删除失败'
            ); 
        }
        $this->ajaxReturn($result);
    }


}

